/**
 * Created by zhangqingxi on 2017-04-25.
 */
var modelPath=ROOT_PATH+'/environmental/solidWaste/storageManager/';

//初始化部门
function initDept() {

    $('#deptId').combotree({
        url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0',
        required:true,
        editable:false,
        prompt:'请选择'
    });
}
//初始化固废类型
function initSolidWasteType() {

    $('#solidWasteTypeId').combobox({
        data:[{"id":0,"text":"一般"},{"id":1,"text":"危废"}],
        valueField:'id',
        textField:'text',
        panelHeight:'auto',
        editable:false,
        required:true,
        onChange:function (newValue,oldValue) {
            $('#solidWasteId').combobox({
                value:''
            });
            $('#hazardousWasteCodeId').combogrid({
                value:''
            });
            $('#hazardousWasteContent').textbox('setValue','');
            var required=false;
            if(newValue==1){
                required=true;
            }
            initSolidWaste(newValue,required);
            resetCombogrid('#hazardousWasteCodeId')
        }
    });
}

//初始化固废名称solidWasteId
function initSolidWaste(solidWasteTypeId,required) {
    $('#solidWasteId').combobox({
        url: solidWasteByTypeId+solidWasteTypeId,
        valueField:'id',
        textField:'text',
        required:true,
        prompt:'请选择',
        editable:false,
        onChange:function (newValue,oldValue) {
            $('#hazardousWasteCodeId').combogrid({
                value:''
            });
            $('#hazardousWasteContent').textbox('setValue','');
            if(required){
                findSolidWasteById(newValue);
            }

        }
    });
}

function findSolidWasteById(id) {

    $.ajax({
        url: solidWasteById+id,
        type:'POST',
        dataType:'json',
        success: function (data) {
            var hazardousWasteTypeId=data.hazardousWasteTypeId;
            initHazardousWasteCode(hazardousWasteTypeId);
        }
    });
}

//初始化危废代码 hazardousWasteCodeId
function initHazardousWasteCode(hazardousWasteTypeId) {

    $('#hazardousWasteCodeId').combogrid({
        required:true,
        editable:false,
        readonly:false,
        panelWidth:500,
        prompt:'请选择',
        idField:'id',
        textField:'code',
        queryParams:{
            sort:'d.addTime',
            order:'desc',
            hazardousWasteTypeId:hazardousWasteTypeId
        },
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        sortOrder:true,
        striped:true,
        url:hazardousWasteCodeByParam,
        columns:[[
            {field:'code',title:'危废代码',width:60},
            {field:'remarks',title:'危废描述',width:100}
        ]],
        onSelect:function (index, row) {
            $('#hazardousWasteContent').textbox('setValue',row.remarks);
        }
    });
}

//初始化形态 shapeId
function initShape() {
    $('#shapeId').combobox({
        url:dictionaryByCode+dictionaryShape,
        required:true,
        prompt:'请选择',
        editable:false,
        valueField:'id',
        textField:'text'
    });
}

//初始化产生源
function initGenerationSource() {
    $('#generationSourceId').combobox({
        url:dictionaryByCode+dictionaryGenerationSource,
        required:true,
        editable:false,
        prompt:'请选择',
        valueField:'id',
        textField:'text'
    });
}


//固废类型加载之后的事件
function solidWasteTypeLoadSuccess() {

    var required=false;
    var $solidWasteTypeIdValue=$('#solidWasteTypeId').val();
    if($solidWasteTypeIdValue==1){
        required=true;
    }
    solidWasteLoadSuccess(required);
    initSolidWaste($solidWasteTypeIdValue,required);
    //加载固废类型
    initSolidWasteType();
}
//固废名称加载成功之后的事件
function solidWasteLoadSuccess(required) {
    var $solidWasteIdValue=$('#solidWasteId').val();
    if(required){
        findSolidWasteById($solidWasteIdValue,required);
        hazardousWasteCodeLoadSuccess();
    }else{
        resetCombogrid('#hazardousWasteCodeId');
    }
}
//危废代码加载完成之后的事件
function hazardousWasteCodeLoadSuccess() {
    $('#hazardousWasteCodeId').combogrid({
        //加载完成危废代码之后显示描述
        onLoadSuccess:function (index, row) {
            var $gridObj = $('#hazardousWasteCodeId').combogrid('grid');	// 获取数据表格对象
            var $SelectedRow = $gridObj.datagrid('getSelected');	// 获取选择的行
            if($SelectedRow){
                $('#hazardousWasteContent').textbox('setValue',$SelectedRow.remarks);
            }
        }
    });

}

/**
 * 加载表格
 *
 */
function  loadDataList(url) {

    $('#dataList').datagrid({
        url:url,
        queryParams:{
            sort:'d.addTime',
            order:'desc'
        },
        // idField:'id',
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        sortOrder:true,
        striped:true,
        fit:true,
        columns:[[
            {field:'id',width:10,checkbox:true},
            {field:'solidWasteName',title:'固废名称',width:10,sortable:false,formatter:function (value,row,index) {
                return '<a href="javascript:void(0)" onclick="showDetails(\''+row.id+'\')" style="color: blue">'+value+'</a>'

            }},
            {field:'solidWasteTypeName',title:'固废类别',width:10,sortable:false},
            {field:'hazardousWasteCode',title:'危废代码',width:10,sortable:false},
            {field:'vulgo',title:'俗称',width:10,sortable:false},
            {field:'number',title:'贮存数量(吨)',width:10,sortable:false},
            {field:'addUserName',title:'登记人',width:10,sortable:false},
            {field:'addDeptName',title:'登记部门',width:10,sortable:false},
            {field:'d.addTime',title:'登记时间',width:10,sortable:true,formatter:function (value,row,index) {
                return row.addTime;
            }},
            {field:'remarks',title:'备注',width:10,sortable:false}
        ]],
        toolbar: '#toolbar'

    });
}
